package com.dequan.springaiproject.controller;


import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 模型参数设置
 * 1. 使用 mutate() 在 ChatModel 的基础做小改动满足个别的需求
 * 2. chatClient & charModel % openAiApi
 *                           % openAiChatOptions
 */
@RestController
@RequestMapping("/mutate")
public class MutateController {

    @Autowired
    private OpenAiChatModel chatModel;

    @Autowired
    private OpenAiApi baseOpenAiApi;

    @GetMapping("/chat")
    public String setBaseOpenAiApi() {

        // Derive a new OpenAiApi for Groq (Llama3)
        // OpenAiApi groqApi = baseOpenAiApi.mutate()
        //         .baseUrl("https://api.groq.com/openai")
        //         .apiKey(System.getenv("GROQ_API_KEY"))
        //         .build();

        // 设置 API Key 和 Base URL
        OpenAiApi openAiApi = baseOpenAiApi.mutate()
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode")
                .apiKey("sk-f57d7ba62a574c7c9741940491a92897")
                .build();

        // 设置 模型选项
        OpenAiChatOptions openAiChatOptions = OpenAiChatOptions.builder()
                .model("qwen-plus")
                .temperature(0.7)
                .build();

        // 创建 ChatModel
        OpenAiChatModel openAiChatModel = chatModel.mutate()
                .openAiApi(openAiApi)
                .defaultOptions(openAiChatOptions)
                .build();

        String prompt = "What is the capital of France?";

        return ChatClient.builder(openAiChatModel).build()
                .prompt(prompt)
                .call()
                .content();
    }
}
